<template>
	<view class="app">
		<view class="price-box">
			<text>支付金额</text>
			<text class="price">{{orderInfo.orderPrice}}</text>
		</view>

		<view class="pay-type-list">

			<view class="type-item b-b" @click="changePayType(1)">
				<text class="icon yticon icon-weixinzhifu"></text>
				<view class="con">
					<text class="tit">微信支付</text>
					<text>推荐使用微信支付</text>
				</view>
				<label class="radio">
					<radio value="" color="#fa436a" :checked='payType == 1' />
					</radio>
				</label>
			</view>
			
			<view class="type-item" @click="changePayType(2)">
				<text class="icon yticon icon-erjiye-yucunkuan"></text>
				<view class="con">
					<text class="tit">预存款支付</text>
					<text>可用余额 ¥{{userinfo.balance}}</text>
				</view>
				<label class="radio">
					<radio value="" color="#fa436a" :checked='payType == 2' />
					</radio>
				</label>
			</view>
		</view>
		
		<text class="mix-btn" @click="confirm" v-if="!zhifuzhong">确认支付</text>
		<text class="mix-btn" v-else>支付中...</text>
	</view>
</template>

<script>
	//import weixinpay from '../../util/weixinpay.js'
	export default {
		data() {
			return {
				payType: 1,
				orderInfo: {},
				userinfo:[],
				order_number:[],
				zhifuzhong:false
			};
		},
		computed: {
		
		},
		onLoad(options) {
			//console.log(option.source);
			this.order_number = options.order_number;
			
			uni.getStorage({
			    key: 'userInfo',
			    success:(res)=> {
					//console.log(res.data);
			         this.userinfo=res.data
			    }
			});
		
			this.getorderinfo(options);
		},
onShow() {
		this.getuserinfo();
	const res=  uni.getStorage({
	    key: 'userInfo',
	    success: (res)=> {
			
			if(res.data){
				//console.log(res.data,'ssssss')
	     this.userinfo=res.data
		 if(this.userinfo.member_id){
		// this.getuseragain();
		 //this. url="http://pth5.njkw668.com/#/pages/user/user?&pid="+this.userinfo.member_id
		 }
		 }
		 else{
			 window.location.href='https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx9a8339b68f86cd01&redirect_uri=http%3A%2F%2Fpth5.njkw668.com%2F%23%2Fpages%2Fpublic%2Flogin&response_type=code&scope=snsapi_userinfo&state=0#wechat_redirect';
		 }
	    },
		fail: (res)=> {
		window.location.href='https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx9a8339b68f86cd01&redirect_uri=http%3A%2F%2Fpth5.njkw668.com%2F%23%2Fpages%2Fpublic%2Flogin&response_type=code&scope=snsapi_userinfo&state=0#wechat_redirect';
		}
	});
},
		methods: {
			//选择支付方式
			changePayType(type) {
				this.payType = type;
			},
			async  getuserinfo(){
				const res= await this.$myRequest({
					url:'index.php/api/user/getuserinfo',
					method:"post",
					data:{
						"member_id":this.userinfo.member_id
					}
					
				})
				this.userinfo=res.data;
			},
			async  getorderinfo(options){
				const res= await this.$myRequest({
					url:'index.php/api/order/orderinfo',
					method:"post",
					data:{
						"member_id":this.userinfo.member_id,
						"order_number":options.order_number
					}
					
				})
				this.orderInfo=res.data;
			},
			//确认支付
			async confirm () {
				this.zhifuzhong=true
				//function(respay) {
				// console.log('res', respay)
				// if (respay.err_msg === "get_brand_wcpay_request:ok") {
				// uni.redirectTo({
				// url:"../order/order"
				// })
				// } else if (respay.err_msg === "get_brand_wcpay_request:cancel") {
				// uni.showToast({
				// title:"取消支付",
				// icon:"none",
				// duration:2000
				// })
				// } else if (respay.err_msg === "get_brand_wcpay_request:fail") {
				// uni.showToast({
				// title:"支付失败",
				// icon:"none",
				// duration:2000
				// })
				// }
				// }, function(err) {
				// uni.showToast({
				// title:err,
				// icon:"none",
				// duration:2000
				// })
				// })
				//console.log(this.userinfo);
				
				const res= await this.$myRequest({
					url:'index.php/api/order/pay',
					method:"post",
					data:{
						"member_id":this.userinfo.member_id,
						"order_number":this.order_number,
						"type":this.payType
					}
					
				})
				if(this.payType==1){
				// uni.showToast({
				// title:res.data,
				// icon:"none",
				// duration:2000
				// })
				// return uni.showToast({
				// 	title:res.status
				// })
				// console.log(res.data);
				if(res.data.code==200){
					//console.log(WeixinJSBridge.invoke,'我有那些方法')
					WeixinJSBridge.invoke(
					'getBrandWCPayRequest',{
					//provider:'wxpay',
					"appId": res.data.appId, //公众号名称，由商户传入
					"timeStamp":res.data.timeStamp, //时间戳
					"nonceStr":res.data.nonceStr, //随机串
					"package": res.data.package, //扩展包
					"signType":res.data.signType, //微信签名方式:MD5
					"paySign":res.data.paySign ,//微信签名
						
						},
						 function(res){
						
						                if (res.err_msg == "get_brand_wcpay_request:ok") {
						
						                    setTimeout((
												uni.showToast({
													title:'支付成功'
												})
											),2000)
						uni.switchTab({
						                url: '/pages/money/paySuccess',
						    });
						                    }
											}
										
					 )
					 
					 
				}
				}else if(this.payType==2){
					console.log(res);
								
										if(res.status==200){
											setTimeout((
														uni.showToast({
														title:res.data,
														icon:"none",
														duration:2000
														})
													),2000)
													if(res.data=='付款成功'){
														uni.navigateTo({
											                url: '/pages/money/paySuccess',
														});
													}
											
										}else{
											uni.showToast({
											title:'支付失败',
											icon:"none",
											duration:2000
											})
										}
					
					        }
				
			},
		}
	}
</script>

<style lang='scss'>
	.app {
		width: 100%;
	}

	.price-box {
		background-color: #fff;
		height: 265upx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		font-size: 28upx;
		color: #909399;

		.price{
			font-size: 50upx;
			color: #303133;
			margin-top: 12upx;
			&:before{
				content: '￥';
				font-size: 40upx;
			}
		}
	}

	.pay-type-list {
		margin-top: 20upx;
		background-color: #fff;
		padding-left: 60upx;
		
		.type-item{
			height: 120upx;
			padding: 20upx 0;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding-right: 60upx;
			font-size: 30upx;
			position:relative;
		}
		
		.icon{
			width: 100upx;
			font-size: 52upx;
		}
		.icon-erjiye-yucunkuan {
			color: #fe8e2e;
		}
		.icon-weixinzhifu {
			color: #36cb59;
		}
		.icon-alipay {
			color: #01aaef;
		}
		.tit{
			font-size: $font-lg;
			color: $font-color-dark;
			margin-bottom: 4upx;
		}
		.con{
			flex: 1;
			display: flex;
			flex-direction: column;
			font-size: $font-sm;
			color: $font-color-light;
		}
	}
	.mix-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 630upx;
		height: 80upx;
		margin: 80upx auto 30upx;
		font-size: $font-lg;
		color: #fff;
		background-color: $base-color;
		border-radius: 10upx;
		box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
	}

</style>
